<template>
	<view class="content">
		<view v-if="list.length > 0" class="user-list">
			<view v-for="(item, index) in list" :key="index" class="user-item" @click="handle(item)">
				<view class="user-avatar">
					<image class="avatar" :src="getAvatarUrl(item.avatar)" mode="aspectFill"></image>
				</view>
				<view class="info-wrap">
					<view class="user-info">
						<view class="name">
							<text>{{item.nick}}</text>
						</view>
						<view class="info">
							<view v-if="!!item.gender" :class="['sex-wrap', 'sex' + getSexValue(item.gender)]">
								<image class="sex-icon" :src="'/static/img/sex' + getSexValue(item.gender) + '.png'" mode=""></image>
							</view>
						</view>
					</view>
				</view>
				<view class="uni-icon uni-icon-more"></view>
			</view>
		</view>
		<view v-else class="empty-wrap">
			<image class="empty-icon" src="/static/img/empty.png" mode=""></image>
			<text class="empty-text">无黑名单数据</text>
		</view>
	</view>
</template>

<script>
	import util from "@/common/util.js"
	export default {
		data() {
			return {
				myInfo: {},
				list: [],
			}
		},
		onLoad(options) {
			try {
				const myInfo = uni.getStorageSync('myInfo');
				if (myInfo) {
					this.myInfo = myInfo;
				}
			} catch (e) {
				console.log(e)
			}
		},
		onReady() {
			this.getBlacklist()
		},
		onPullDownRefresh() {
			setTimeout(() => {
				this.getBlacklist();
			}, 500)
		},
		methods: {
			getBlacklist: function () {
				uni.showLoading({
					title: ''
				});
				this.$tim.getBlacklist().then(imResponse => {
					console.log('我的黑名单列表', imResponse.data);
					uni.stopPullDownRefresh();
					if (imResponse.data.length > 0) {
						this.$tim.getUserProfile({
							userIDList: imResponse.data
						}).then(imResponse => {
							uni.hideLoading();
							this.list = imResponse.data
							console.log('存储用户资料的数组', JSON.stringify(imResponse.data));
						}).catch(imError => {
							console.warn('获取其他用户资料失败', imError);
						});
					} else {
						this.list = []
						uni.hideLoading();
					}
				}).catch(imError => {
					console.warn('获取黑名单列表失败', imError);
				});
			},
			
			getSexValue: function (value) {
				if (value == 'Gender_Type_Male') {
					return '1'
				}
				if (value == 'Gender_Type_Female') {
					return '2'
				}
			},
			
			getAvatarUrl: function (url) {
				if (url.indexOf(this.QINIUURL) == -1) {
					return url;
				} else {
					return url + '?imageView2/1/w/100/h/100';
				}
			},
			
			handle: function (item) {
				let to_uid = util.getUserID(item.userID, this.IDBEFORE)
				let user = {
					id: to_uid,
					name: item.nick,
					avatar:item.avatar,
				}
				uni.showActionSheet({
					itemList: ['查看详情', '取消拉黑'],
					success: res => {
						if (res.tapIndex == 0) {
							uni.navigateTo({
								url: '../home/user_home?user=' + JSON.stringify(user)
							})
						}
						if (res.tapIndex == 1) {
							uni.showModal({
								title: '',
								content: '确认要把'+user.name+'从黑名单移除？',
								success: res => {
									if (res.confirm) {
										uni.showLoading({
											title: ''
										});
										this.$api.api.ajaxData({
											method: 'V1.BlackUser',
											uid: this.myInfo.id,
											token: this.myInfo.token,
											touid: user.id,
										}, res => {
											uni.hideLoading();
											if (res.status) {
												this.$tim.removeFromBlacklist({
													userIDList: [item.userID],
												}).then(imResponse => {
													uni.showToast({
														title: '成功把'+user.name+'从黑名单移除',
														position: 'bottom',
														icon: 'none'
													})
													setTimeout(() => {
														this.getBlacklist();
													}, 1000)
													console.log('从黑名单中成功移除', imResponse.data);
												}).catch(imError => {
													console.warn('从黑名单中移除失败', imError);
												});
											} else {
												uni.showToast({
													title: res.msg,
													position: 'bottom',
													icon: 'none'
												})
											}
										}, err => {
											uni.hideLoading();
											uni.showToast({
												title: '网络错误，请稍后再试',
												position: 'bottom',
												icon: 'none'
											});
										})
									}
								}
							});
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import '../../common/common.scss';
	page {
		background-color: #FFFFFF;
	}
	.user-item {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		padding: 20rpx;
		background-color: #fff;
		.user-avatar {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 1rpx;
			border: 4rpx solid #fff;
			border-radius: 120rpx;
			position: relative;
			.avatar {
				width: 100rpx;
				height: 100rpx;
				border-radius: 100rpx;
				background-color: $image-bg-color;
			}
		}
		.info-wrap {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			flex: 1;
			height: 140rpx;
			border-bottom: 1rpx solid #f9f9f9;
		}
		.user-info {
			flex: 1;
			margin: 0 20rpx;
			.name {
				display: flex;
				justify-content: flex-start;
				align-items: center;
				text {
					max-width: 360rpx;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					font-size: 36rpx;
					color: #333;
				}
			}
			.info {
				display: flex;
				justify-content: flex-start;
				align-items: center;
			}
			.sex-wrap {
				display: flex;
				justify-content: center;
				align-items: center;
				line-height: 28rpx;
				padding: 5rpx 10rpx;
				border-radius: 28rpx;
				&.sex1 {
					background: #83d7ff;
				}
				&.sex2 {
					background: #fe9ab7;
				}
				.sex-icon {
					width: 28rpx;
					height: 28rpx;
				}
				.age {
					font-size: 28rpx;
					color: #fff;
					margin-left: 5rpx;
				}
			}
			.city-wrap {
				display: flex;
				justify-content: center;
				align-items: center;
				line-height: 28rpx;
				padding: 5rpx 10rpx;
				border-radius: 28rpx;
				background: #4cbcfc;
				margin-left: 10rpx;
				.city-icon {
					width: 28rpx;
					height: 28rpx;
				}
				.city {
					font-size: 28rpx;
					color: #fff;
					margin-left: 5rpx;
				}
			}
			.intro {
				width: 360rpx;
				font-size: 32rpx;
				color: #999999;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
				margin-top: 10rpx;
			}
		}
		.uni-icon {
			font-size: 64rpx;
			color: #666666;
		}
	}
</style>
